var app = getApp()
var common_Js = require("../../component/common.js"), fetchApi = require("../../utils/httpPost.js")
import Toast from '../../component/vant/toast/toast';
const CryptoJS = require("../../utils/aesUtil.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl:app.getImgUrl(),
    current: 0,
    currentTabId: 0,
    pageIndex: 1,
    pageSize: 10,
    tabs: [],//标题tab菜单
    sections: [],//内容行集合
    loading: false,
    netWork: true,
    isLoading: true,
    isExceed: false,
    merList:[],
    hotword:'请输入商家名称',
    searchValue:'',
    isLogin:0,
    wd:0,
    jd:0,
    activity: [],
    districts:[],
    districtid:0,
    cate_child_id:0,
    value1:'0',
    sortType:'default',
    serviceTypes: [
      { text: '全部商品', value: 0 },
      { text: '新款商品', value: 1 },
      { text: '活动商品', value: 2 }
    ],
    sortTypes: [
      { text: '默认排序', value: 'default' },
      { text: '好评排序', value: 'haoping' },
      { text: '销量排序', value: 'sale' }
    ],
  },
  handleChange({ detail }) {
    console.log('detail', detail)
    var that = this
    var tab_index = 0
    that.setData({ current: detail.index });
    var tabs_temp = that.tabs
    for (var index in tabs_temp) {
      if (detail.key == "tab" + tabs_temp[index].id) {
        that.currentTabId = tabs_temp[index].id
      }
    }
    for (var ind in that.data.districts){
      if (detail.index == ind) {
        tab_index = ind
      }
    }
    that.data.districtid = that.data.districts[tab_index].id
    console.log('that.data.districtid', that.data.districtid)
    that.data.pageIndex = 1
    that.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      merList:[]
    })
    that.onHttpLoadMore()
  },
  modalcnt:function  ()  {
    var  that  =  this
    that.data.pageIndex = 1
    //获取经纬度  
    wx.getLocation({
      type:  'gcj02',
      success:  function  (res)  {
        console.log('modalcnt', res)
        var latitude = res.latitude
        var longitude = res.longitude
        that.data.wd = latitude//纬度
        that.data.jd = longitude//经度
        that.setData({
          wd : latitude,
          jd : longitude
        })
        that.onHttpInit()
      },fail:function(res){
        that.data.wd = 0
        that.data.jd = 0
        that.onHttpInit()
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log('options',options)
    if(options.type){
      var cate_child_id =  parseInt(options.type)
      this.data.cate_child_id = cate_child_id;
    }

    var o = getCurrentPages().pop();
    console.log('onLoad o',o)
    var isLogin = app.getStorageSync("isLogin");
    that.setData({ 
      isLogin: isLogin,
    })
    if(this.data.cate_child_id > 0){
      that.setData({ 
        value1: this.data.cate_child_id + '',
      })
    }
    if(options.title){
      var title = options.title;
      wx.setNavigationBarTitle({
        title: title,
      })
    }
    this.initRequest();
  },
  onShow: function () {
  },
  initRequest(){
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      merList:[]
    })
    this.modalcnt()
  },
  onChange(val){
    console.log('onChange',val)
    this.setData({
      searchValue: val.detail
    });
  },
  onSearch(val){
    console.log('onSearch',val)
    this.data.searchValue = val.detail;
    this.initRequest()
  },
  onSearchClick(val){
    console.log('onSearchClick',val)
    this.initRequest()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/mer/list',{
      latitude: that.data.wd,
      longitude: that.data.jd,
      pageIndex: that.data.pageIndex,
      pageSize: that.data.pageSize,
      cate_id: 2,
      cate_child_id:that.data.cate_child_id,
      keyword:that.data.searchValue,
      sort:that.data.sortType
    }, function (res) {
      console.log("获取商家列表", res)
      if(res.data.totalcount > 0){
        that.data.pageIndex = that.data.pageIndex + 1;
        var mers = res.data.data.mers;
        if(mers.length > 0){
          for (var index in mers) {
            that.data.merList.splice(that.data.merList.length, 0, mers[index]);
          }
        }
      }
      // that.data.serviceTypes = res.data.data.serviceTypes,
      that.setData({ 
        serviceTypes:res.data.data.serviceTypes,
        merList: that.data.merList,
      });
      
      for(var index in that.data.serviceTypes){
        if(parseInt(that.data.cate_child_id) == that.data.serviceTypes[index].value){
          var title = that.data.serviceTypes[index].text;
          wx.setNavigationBarTitle({
            title: title,
          })

          that.setData({
            value1: that.data.serviceTypes[index].value
          })
        }
      }

      that.setData({ isLoading: false, loading: true, netWork: true })
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    },function(){})
  },
  resetTime: function (coupons) {
    for (var index in coupons) {
      var b = coupons[index].beginTime//.replace(/-/g, '/');
      var e = coupons[index].endTime//.replace(/-/g, '/');
      coupons[index].beginTime = b.substring(0, 10);//common_Js.formatTimeTwo(dateBegin, 'Y-M-D');
      coupons[index].endTime = e.substring(0, 10);//common_Js.formatTimeTwo(dateEnd, 'Y-M-D')
    }
    return coupons;
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作  下拉刷新
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      merList:[],
      netWork: false,
      isLoading: true
    })
    this.onHttpInit()
  },
  changeServiceType(res){
    console.log('changeServiceType',res)
    var type = res.detail;
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      merList:[],
      netWork: false,
      isLoading: true
    })
    for(var index in this.data.serviceTypes){
      if(parseInt(type) == this.data.serviceTypes[index].value){
        var title = this.data.serviceTypes[index].text;
        wx.setNavigationBarTitle({
          title: title,
        })

        this.setData({
          value1: this.data.serviceTypes[index].value
        })
      }
    }
    this.data.cate_child_id = parseInt(res.detail);
    this.onHttpInit()
  },
  changeSortType(res){
    console.log('changeSortType',res)
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      merList:[],
      netWork: false,
      isLoading: true
    })
    this.data.sortType = res.detail;
    this.onHttpInit()
  },
  /**
   * 页面上拉触底事件的处理函数    加载更多
   */
  onReachBottom: function () {
    this.onHttpLoadMore()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpLoadMore: function () {
    console.log('onHttpLoadMore')
    var that = this
    fetchApi.httpPost('/mer/list',{
      latitude: that.data.wd,
      longitude: that.data.jd,
      pageIndex: that.data.pageIndex,
      pageSize: that.data.pageSize,
      cate_id: 2,
      cate_child_id:that.data.cate_child_id
    }, function (res) {
      console.log("加载更多数据", res)
      that.data.pageIndex = that.data.pageIndex + 1
      var mers = res.data.data.mers;
      if(mers.length > 0){
        for (var index in mers) {
          that.data.merList.splice(that.data.merList.length, 0, mers[index]);
        }
        that.setData({ merList: that.data.merList });
      }
      that.setData({ isLoading: false, loading: true, netWork: true })
      // complete
      wx.stopPullDownRefresh() //停止下拉刷新
    },function(){})
  },
})